package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    @Autowired
    private ApArticleMapper apArticleMapper;
    public static final int DEFAULT_SIZE = 10;
    public static final int MAX_SIZE = 50;
    /**
     * 按条件加载文章列表
     * @param dto
     * @param type 1 1:加载更多 loadmore,2:加载最新 loadnew
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, Short type) {
        // 1. 校验参数
        // 1.1 校验分页参数
        if (dto.getSize() == null || dto.getSize() <= 0){
            dto.setSize(DEFAULT_SIZE);
        }
        int size = Math.min(dto.getSize(), MAX_SIZE);
        dto.setSize(size);
        // 1.2 校验type
        if (type != ArticleConstants.LOADTYPE_LOAD_MORE || type != ArticleConstants.LOADTYPE_LOAD_NEW){
            type = ArticleConstants.LOADTYPE_LOAD_NEW;
        }
        // 1.3 校验tag
        if (StringUtils.isBlank(dto.getTag())){
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        // 1.4 校验时间
        if (dto.getMinBehotTime() == null){
            dto.setMinBehotTime(new Date());
        }
        if (dto.getMaxBehotTime() == null){
            dto.setMaxBehotTime(new Date());
        }
        // 2. 调用mapper进行查询
        List<ApArticle> apArticleList = apArticleMapper.loadArticleList(dto, type);
        return ResponseResult.okResult(apArticleList);
    }

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;
    /**
     * 保存或修改文章
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {

        // try {
        //     Thread.sleep(4000);
        // } catch (InterruptedException e) {
        //     e.printStackTrace();
        // }
        // 1. 检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);

        if (dto.getId() == null){
            // 2.判断id是否为空，如果为空证明是新增
            // 2.1 向ap_article表保存文章信息

            this.save(apArticle);
            // 2.2 向ap_article_config表保存文章配置(默认值)
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(apArticleConfig);
            // 2.3 向ap_article_content表保存文章内容
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(apArticleContent);
        }else {
            // 3. 如果id不为空，证明是修改
            // 3.1 修改ap_article表的文章信息
            this.updateById(apArticle);
            // 3.2 修改ap_article_content表的文章内容
            LambdaQueryWrapper<ApArticleContent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ApArticleContent::getArticleId, apArticle.getId());
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(lambdaQueryWrapper);
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(apArticleContent);
        }
        // 4.生成静态页面并上传到minio
        articleFreemarkerService.buildArticleToMinIO(apArticle,dto.getContent());
        // 5. 封装并返回响应数据(文章的id)
        return ResponseResult.okResult(apArticle.getId());
    }
}
